//Fusion of the 422 Proportion
//Dustin Striplin, Tyler Simrell, Lawrence Grass, and Jacob Hall
//Finished 5/1/13

package view;

import java.awt.BorderLayout;
import java.util.Observable;
import java.util.Observer;

import javax.swing.JFrame;
import javax.swing.JPanel;

import model.PageInfo;
import controller.MainController;

/**
 * The ReporterView class for the webcrawler program
 * @author Lawrence Grass
 * @version 4/28/13
 */
@SuppressWarnings("serial")
public class ReporterView extends JFrame
						  implements Observer{
	
	/**
	 * Sets the width of the application
	 */
	public static final int FRAME_WIDTH = 800;
	
	/**
	 * Sets the height of the application
	 */
	public static final int FRAME_HEIGHT = 640;
	
	/**
	 * The panel the holds the different options for the web crawl
	 */
	FormPanel formPanel;
	
	/**
	 * The panel that shows the crawl results for each page as they are gathered
	 */
	CurrentResultsPanel currentResultsPanel = new CurrentResultsPanel();
	
	/**
	 * The controller that coordinates between our model and the view
	 */
	MainController controller = new MainController();
	
	/**
	 * Creates a new ReporterView, which is the base frame for this app
	 */
	public ReporterView(){
		super("TCSS 422 WebCrawler by Fusion Of The 422 Proportion");
		this.setResizable(false);		
		this.setSize(FRAME_WIDTH, FRAME_HEIGHT);
		this.setDefaultCloseOperation(EXIT_ON_CLOSE);
		
		//lets us observe the controller to intercept when a new PageInfo finishes
		controller.addObserver(this);
		
		JPanel mainView = new JPanel();
		mainView.setLayout(new BorderLayout());
		
		//sets up the form panel and adds it
		formPanel = new FormPanel(controller, this);
		this.add(formPanel, BorderLayout.EAST);
		
		//adds the view that shows our current results
		this.add(currentResultsPanel, BorderLayout.CENTER);
	}
	
	/**
	 * When notified updates our current results panel with the new page info
	 */
	@Override
	public synchronized void update(Observable controller, Object pageInfo) {
		if(pageInfo.getClass() == PageInfo.class) {
			formPanel.updateProgress();
			// this calls an update method that is used to control 
			// the progress bar (percentage of work complete)
			currentResultsPanel.insertResult((PageInfo) pageInfo);
		}	
	}
	public void resetCurrentResults(){
		this.remove(currentResultsPanel);
		currentResultsPanel = new CurrentResultsPanel();
		this.add(currentResultsPanel);
		this.revalidate();
	}
}
